package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.Massif;
import com.lsyw.intergrate.entity.MassifUser;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.service.MassifUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工地人员 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-11-27
 */
@Api(tags = "工地人员")
@RestController
@RequestMapping("/massif-user")
public class MassifUserController {


    @Autowired
    private MassifUserService massifUserService;


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    @ApiOperation(value = "通过id查询", notes = "通过id查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    public Result getById(String id) {
        return Result.successResult(massifUserService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST")
    public Result save(@RequestBody MassifUser massifUser) {
        massifUser.setDelFlag(0);
        massifUserService.save(massifUser);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id")
    @ApiOperation(value = "通过id删除", notes = "通过id删除", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    public Result delete(String id) {
        MassifUser massifUser = massifUserService.getById(Long.parseLong(id));
        massifUser.setDelFlag(1);
        this.massifUserService.updateById(massifUser);
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST")
    public Result updateById(@RequestBody MassifUser massifUser) {
        massifUser.setDelFlag(0);
        massifUser.setGmtModified(new Date());
        massifUserService.updateById(massifUser);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询列表", notes = "查询列表", httpMethod = "POST")
    public Result list(@RequestBody MassifUserReqVo massifUser) {
        final LambdaQueryWrapper<MassifUser> lambda = new QueryWrapper<MassifUser>().lambda();
        this.buildCondition(lambda, massifUser);
        return Result.successResult(massifUserService.list(lambda));
    }

    /**
     * 查询列表
     */
    @GetMapping("/listForRole")
    @ApiOperation(value = "查询列表-根据角色", notes = "查询列表", httpMethod = "GET")
    public Result list(String name,String role) {
        final LambdaQueryWrapper<MassifUser> lambda = new QueryWrapper<MassifUser>().lambda();
        lambda.eq(MassifUser::getDelFlag,0);
        lambda.eq(MassifUser::getRole,role);
        if (!StringUtils.isEmpty(name)) {
            lambda.like(MassifUser::getName,name);
        }

        return Result.successResult(massifUserService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询", notes = "分页查询", httpMethod = "POST")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<MassifUser> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<MassifUser> lambda = new QueryWrapper<MassifUser>().lambda();
        lambda.eq(MassifUser::getDelFlag,0);
        Map<String, Object> condition = pageInfo.getCondition();
        if (condition.get("name") != null && !StringUtils.isEmpty(condition.get("name").toString())) {
            lambda.like(MassifUser::getName, condition.get("name"));
        }
        if (condition.get("massifId") != null && !StringUtils.isEmpty(condition.get("massifId").toString())) {
            lambda.eq(MassifUser::getMassifId, condition.get("massifId"));
        }
        return Result.successResult(this.massifUserService.page(page, lambda));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<MassifUser> lambda, MassifUserReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(MassifUser::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(MassifUser::getName, param.getName());
        }
        if (!StringUtils.isEmpty(param.getPassword())) {
            lambda.eq(MassifUser::getPassword, param.getPassword());
        }
        if (!StringUtils.isEmpty(param.getSalt())) {
            lambda.eq(MassifUser::getSalt, param.getSalt());
        }
        if (!StringUtils.isEmpty(param.getPhone())) {
            lambda.eq(MassifUser::getPhone, param.getPhone());
        }
        if (!StringUtils.isEmpty(param.getRole())) {
            lambda.eq(MassifUser::getRole, param.getRole());
        }
        if (!StringUtils.isEmpty(param.getXb())) {
            lambda.eq(MassifUser::getXb, param.getXb());
        }
        if (!StringUtils.isEmpty(param.getIdCard())) {
            lambda.eq(MassifUser::getIdCard, param.getIdCard());
        }
        if (!StringUtils.isEmpty(param.getPhoto())) {
            lambda.eq(MassifUser::getPhoto, param.getPhoto());
        }
        if (!StringUtils.isEmpty(param.getStatus())) {
            lambda.eq(MassifUser::getStatus, param.getStatus());
        }
        if (!StringUtils.isEmpty(param.getCreatorId())) {
            lambda.eq(MassifUser::getCreatorId, param.getCreatorId());
        }
        if (!CollectionUtils.isEmpty(param.getCreatedTimeList())) {
            lambda.ge(MassifUser::getCreatedTime, param.getCreatedTimeList().get(0));
            lambda.le(MassifUser::getCreatedTime, param.getCreatedTimeList().get(1));
        }
        if (!CollectionUtils.isEmpty(param.getUpdatedTimeList())) {
            lambda.ge(MassifUser::getUpdatedTime, param.getUpdatedTimeList().get(0));
            lambda.le(MassifUser::getUpdatedTime, param.getUpdatedTimeList().get(1));
        }
        if (!StringUtils.isEmpty(param.getUpdatedUser())) {
            lambda.eq(MassifUser::getUpdatedUser, param.getUpdatedUser());
        }
        if (!StringUtils.isEmpty(param.getIsDelete())) {
            lambda.eq(MassifUser::getIsDelete, param.getIsDelete());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(MassifUser::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(MassifUser::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(MassifUser::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(MassifUser::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(MassifUser::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(MassifUser::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(MassifUser::getDelFlag, param.getDelFlag());
        }
        if (!StringUtils.isEmpty(param.getMassifId())) {
            lambda.eq(MassifUser::getMassifId, param.getMassifId());
        }
        lambda.orderBy(true, false, MassifUser::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class MassifUserReqVo extends MassifUser {
        private List<String> createdTimeList; // 创建时间起止
        private List<String> updatedTimeList; // 更新时间起止
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
